From b7752d1f7c3f6b40f8a16d70a5881f379baa3888 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 16 Jun 2017 23:38:54 +0300 Subject: [PATCH] Don't search bins in src if there's a library Ideally we want not to search for binaries there in any case, but that's probably not gonna happen due to backwards comparability. --- src/cargo/util/toml.rs | 19 ++++++++++++------- tests/build.rs | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 66c947f10..8450b4d4d 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1417,7 +1417,7 @@ fn normalize(package_root: &Path, lib_target(&mut ret, lib); } bin_targets(&mut ret, bins, - &mut |bin| inferred_bin_path(bin, package_root, bins.len())); + &mut |bin| inferred_bin_path(bin, lib.is_some(), package_root, bins.len())); if let Some(custom_build) = custom_build { @@ -1440,6 +1440,7 @@ fn normalize(package_root: &Path, } fn inferred_bin_path(bin: &TomlBinTarget, + has_lib: bool, package_root: &Path, bin_len: usize) -> PathBuf { // here we have a single bin, so it may be located in src/main.rs, src/foo.rs, @@ -1450,9 +1451,11 @@ fn inferred_bin_path(bin: &TomlBinTarget, return path.to_path_buf() } - let path = Path::new("src").join(&format!("{}.rs", bin.name())); - if package_root.join(&path).exists() { - return path.to_path_buf() + if !has_lib { + let path = Path::new("src").join(&format!("{}.rs", bin.name())); + if package_root.join(&path).exists() { + return path.to_path_buf() + } } let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name())); @@ -1469,9 +1472,11 @@ fn inferred_bin_path(bin: &TomlBinTarget, return path.to_path_buf() } - let path = Path::new("src").join(&format!("{}.rs", bin.name())); - if package_root.join(&path).exists() { - return path.to_path_buf() + if !has_lib { + let path = Path::new("src").join(&format!("{}.rs", bin.name())); + if package_root.join(&path).exists() { + return path.to_path_buf() + } } let path = Path::new("src").join("bin").join(&format!("main.rs")); diff --git a/tests/build.rs b/tests/build.rs index d13260531..8b74b807f 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -3251,3 +3251,23 @@ fn explicit_bins_without_paths() { assert_that(p.cargo_process("build"), execs().with_status(0)); } + +#[test] +fn no_bin_in_src_with_lib() { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.1.0" + authors = [] + + [[bin]] + name = "foo" + "#) + .file("src/lib.rs", "") + .file("src/foo.rs", "fn main() {}"); + + assert_that(p.cargo_process("build"), + execs().with_status(101) + .with_stderr_contains(r#"[ERROR] couldn't read "src[/]bin[/]main.rs"[..]"#)); +} -- 2.30.2